import { request } from '../request'
import type {
    Product,
    Category,
    ProductSearchRequest,
    ProductSearchResponse,
    PageRequest,
    PageResponse
} from '@/types'

// ================= 前台 API =================

// 分页查询商品
export const getProductList = (params: Partial<PageRequest> & { categoryId?: number } = {}): Promise<PageResponse<Product>> => {
    return request.get('/api/product-service/api/product', { params })
}

// 获取商品详情
export const getProductDetail = (id: number): Promise<Product> => {
    return request.get(`/api/product-service/api/product/${id}`)
}

// 获取热门商品
export const getHotProducts = (): Promise<Product[]> => {
    return request.get('/api/product-service/api/product/hot')
}

// 搜索商品
export const searchProducts = (data: ProductSearchRequest): Promise<ProductSearchResponse> => {
    return request.post('/api/product-service/api/product/search', data)
}

// 获取分类树
export const getCategoryTree = (): Promise<Category[]> => {
    return request.get('/api/product-service/api/category/tree')
}

// 获取分类详情
export const getCategoryDetail = (id: number): Promise<Category> => {
    return request.get(`/api/product-service/api/category/${id}`)
}

// 查询商品库存
export const getProductStock = (productId: number): Promise<number> => {
    return request.get(`/api/product-service/api/stock/${productId}`)
}


// ================= 后台 API =================

// 分页查询后台商品列表
export const getAdminProductList = (params: PageRequest): Promise<PageResponse<Product>> => {
    return request.get('/api/product-service/admin/product/list', { params })
}

// 创建商品
export const createProduct = (data: Partial<Product>): Promise<number> => {
    return request.post('/api/product-service/admin/product', data)
}

// 更新商品
export const updateProduct = (id: number, data: Partial<Product>): Promise<void> => {
    return request.put(`/api/product-service/admin/product/${id}`, data)
}

// 删除商品
export const deleteProduct = (id: number): Promise<void> => {
    return request.delete(`/api/product-service/admin/product/${id}`)
}

// 创建商品分类
export const createCategory = (data: Partial<Category>): Promise<number> => {
    return request.post('/api/product-service/admin/category', data)
}

// 更新商品分类
export const updateCategory = (id: number, data: Partial<Category>): Promise<void> => {
    return request.put(`/api/product-service/admin/category/${id}`, data)
}

// 删除商品分类
export const deleteCategory = (id: number): Promise<void> => {
    return request.delete(`/api/product-service/admin/category/${id}`)
}
